<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;
use App\Models\Squad;
use App\Models\Crouse;
use App\Models\Classroom;
use App\Models\Notice;


class My_schedule extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'auto_make_schedules';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //获取当前时间是周几
        if (date("w")==0) {
            $week = '7';
        }else{
            $week = date("w");
        }
        //获取当天的所有课程信息
        $crousesheet_ids = DB::table('crousesheets')->where('status','=',1)->where('section',$week)->pluck('id')->toArray();
        //dd($crousesheet_ids);
        $data = [];
        $endtime = date('Y-m-d H:i:s',strtotime('+7 days'));
        for ($i=0; $i <count($crousesheet_ids) ; $i++) {
            $crousesheet_id = $crousesheet_ids[$i];

            $squads_id = DB::table('crousesheets')->where('id',$crousesheet_id)->value('squads_id');
            $crouse_id = DB::table('crousesheets')->where('id',$crousesheet_id)->value('crouse_id');
            $classroom_id = DB::table('crousesheets')->where('id',$crousesheet_id)->value('classroom_id');
            $start_time = DB::table('crousesheets')->where('id',$crousesheet_id)->value('start_time');
            $end_time = DB::table('crousesheets')->where('id',$crousesheet_id)->value('end_time');
            //$period = DB::table('crousesheets')->where('id',$crousesheet_id)->value('period');
            $section = DB::table('crousesheets')->where('id',$crousesheet_id)->value('section');
            $tutor = DB::table('crousesheets')->where('id',$crousesheet_id)->value('tutor_id');
            $squad_name = Squad::where('id',$squads_id)->value('name');
            $crouse_name = Crouse::where('id',$crouse_id)->value('name');
            $classroom_name = Classroom::where('id',$classroom_id)->value('name');
            //$period_name = Crouse_time::find($period)->name;
            //dd($period_name);
            switch ($section) {
                case '1':
                    $section_name = '周一';
                    break;
                case '2':
                    $section_name = '周二';
                    break;
                case '3':
                    $section_name = '周三';
                    break;
                case '4':
                    $section_name = '周四';
                    break;
                case '5':
                    $section_name = '周五';
                    break;
                case '6':
                    $section_name = '周六';
                    break;
                case '7':
                    $section_name = '周日';
                    break;
                default:
                    $section_name = 'N/A';
                    break;
            }
            $attendance_data[] = [
                'notice_type' =>'1',
                'crousesheet_id' =>$crousesheet_ids[$i],
                'event' =>'考勤-'.$squad_name.'-'.$crouse_name.'/'.date('Y-m-d').'/'.$section_name.' '.$start_time.'-'.$end_time.'-'.$classroom_name,
                'starttime' =>date("Y-m-d H:i:s"),
                'grace_days'=>'7',
                'endtime' =>$endtime,
                'status'=>'1',
                'squad_id'=>$squads_id,
                'tutor_id'=>$tutor
            ];
            $jiaxiao_data[] = [
                'notice_type' =>'3',
                'crousesheet_id' =>$crousesheet_ids[$i],
                'event' =>'家校-'.$squad_name.'-'.$crouse_name.'/'.date('Y-m-d').'/'.$section_name.' '.$start_time.'-'.$end_time.'-'.$classroom_name,
                'starttime' =>date('Y-m-d H:i:s',strtotime('+5 day')),
                'grace_days'=>'5',
                'endtime' =>date('Y-m-d H:i:s',strtotime('+10 day')),
                'status'=>'1',
                'squad_id'=>$squads_id,
                'tutor_id'=>$tutor
            ];
            $zuoye_data[] = [
                'notice_type' =>'5',
                'crousesheet_id' =>$crousesheet_ids[$i],
                'event' =>'作业-'.$squad_name.'-'.$crouse_name.'/'.date('Y-m-d').'/'.$section_name.' '.$start_time.'-'.$end_time.'-'.$classroom_name,
                'starttime' =>date('Y-m-d H:i:s',strtotime('+5 day')),
                'grace_days'=>'5',
                'endtime' =>date('Y-m-d H:i:s',strtotime('+10 day')),
                'status'=>'1',
                'squad_id'=>$squads_id,
                'tutor_id'=>$tutor
            ];
        }
        //dd($jiaxiao_data);
        $attendance_res = Notice::insert($attendance_data);
        

        //$jiaxiao_res = Notice::insert($jiaxiao_data);


        //$zuoye_res = Notice::insert($zuoye_data);
        
    }
}
